package com.meng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanling.meng
 */
@Controller
@RequestMapping
public class IndexController {

    @RequestMapping(value = "/path1/**")
    @ResponseBody
    public Map path1(HttpServletRequest request) {
        Map result = new HashMap(10);
        result.put("info", "path1");
        result.put("data", request.getRequestURI());
        return result;
    }

    @RequestMapping(value = "/path2/{abc:.*}")
    @ResponseBody
    public Map path2(@PathVariable String abc) {
        Map result = new HashMap(10);
        result.put("info", "path2");
        result.put("data", abc);
        return result;
    }

    @RequestMapping(value = "/readByteFile", method = RequestMethod.POST)
    @ResponseBody
    public Map readByteFile(HttpServletRequest request) throws Exception {
        Map result = new HashMap(10);
        result.put("info", "readByteFile");
        InputStream is = request.getInputStream();
        byte[] bytes = StreamUtils.copyToByteArray(is);
        result.put("data1", new String(bytes));
        return result;
    }


}
